/**
 * 
 */
package com.cplsystems.incidencias.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.cplsystems.incidencias.app.utils.HibernateDAOSuportUtil;
import com.cplsystems.incidencias.business.Organizacion;
import com.cplsystems.incidencias.business.Persona;
import com.cplsystems.incidencias.business.Transaccion;
import com.cplsystems.incidencias.services.TransaccionDAO;

/**
 * @author César Palalía López (csr.plz@aisa-automation.com)
 * 
 */
@Repository
public class TransaccionDAOImpl extends HibernateDaoSupport implements
		TransaccionDAO {

	@Autowired
	public void init(final SessionFactory sessionFactory)
			throws DataAccessException {
		setSessionFactory(sessionFactory);
	}

	@Transactional
	public void save(Transaccion transaccion) {
		getHibernateTemplate().save(transaccion);
	}

	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<Transaccion> getTransaccionesPendientes() {
		List<Transaccion> transacciones = getHibernateTemplate().find(
				"FROM Transaccion as t WHERE t.ready = ? ", false);
		return transacciones.size() > 0 ? transacciones : null;
	}

	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<Transaccion> getTransaccionByDate(Date date) {
		List<Transaccion> transacciones = getHibernateTemplate().find(
				"FROM Transaccion as t WHERE t.fecha = ?", date);
		return transacciones.size() > 0 ? transacciones : null;
	}

	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<Transaccion> getTransaccionesByResponsable(
			final Persona responsable) {
		List<Transaccion> transacciones = getHibernateTemplate().find(
				"FROM Transaccion as t WHERE t.responsable = ?", responsable);
		return transacciones.size() > 0 ? transacciones : null;
	}

	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<Transaccion> getTransaccionesByOrganizacion(
			final Organizacion organizacion) {
		List<Transaccion> transacciones = getHibernateTemplate()
				.find("FROM Transaccion as t WHERE t.organizacion = ? AND t.ready = ?",
						organizacion, false);
		return transacciones.size() > 0 ? transacciones : null;
	}

	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<Transaccion> getTransaccionesByEmpleado(Persona persona) {
		List<Transaccion> transacciones = getHibernateTemplate().find(
				"FROM Transaccion as t WHERE t.empleado = ?", persona);
		return transacciones.size() > 0 ? transacciones : null;
	}

	@Transactional
	public void delete(Transaccion transaction) {
		getHibernateTemplate().delete(transaction);
	}

}
